package core.java.collections;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

/**
 * @author DGW
 * @date 2017 2017??4??19?? ????8:58:29
 * @filename blockQueueClass.java  
 */
public class blockQueueClass {
	/**
	 * @category
	 * ????blockingqueue ?????????  ??????????
	 * ??????????? ??????    ?????????? concurrent????
	 * ????????    arrayblockingqueue???§ß´ likendblockingqueue (???)
	 */
	public static void main(String[] args) {
		BlockingQueue<Integer> bQueue=new LinkedBlockingQueue<>();
		Thread th1=new Thread(new Producers(bQueue));
		Thread th2=new Thread(new Consumers(bQueue));
		th1.start();
		th2.start();
		
	}
	
	static class Producers implements Runnable{
         private final BlockingQueue<Integer> Queue;
		/**
		 * @param bq
		 */
		public Producers(BlockingQueue<Integer> bq) {
			super();
			this.Queue = bq;
		}

		@Override
		public void run() {
			for(int i=0;i<1000;i++)
			{
				try {
					Queue.put(i);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
                 			
		}
	
	}
	static class Consumers implements Runnable{
		private final BlockingQueue<Integer> Queue;
		
		/**
		 * @param bq
		 */
		public Consumers(BlockingQueue<Integer> bq) {
			super();
			this.Queue = bq;
		}

		@Override
		public void run() {
			for(;;) {
				try {
					System.out.println(Queue.take());
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		
	}

}
